import axios from 'axios'
axios.defaults.withCredentials = true
// 创建axios 实例
export const request = axios.create({
  baseURL: 'https://conduit.productionready.io', // api的base_url
  timeout: 10000 * 10 // 请求超时时间
})
console.log('xxx' ,'-------')
export default ( { store } ) => {

  const { user } = store.state
  // request 拦截器

  request.interceptors.request.use(
    config => {
     console.log(user ,'-useruseruser------')
      if (user && user.token) {
        config.headers.Authorization = `Token ${user.token}`
      }
      // 这里可以自定义一些config配置
      return config
    },
    error => {
      // 处理一些请求出错的情况
      Promise.reject(error)
    }
  )

  // response 拦截器
  request.interceptors.response.use(
    response => {
      const res = response.data
      // const message = res.message || ''
      // if (+res.code !== 0) {
      //   return Promise.reject(message)
      // }
      return res
    },
    (error) => {
      // console.log('err: ' + error.message, JSON.parse(JSON.stringify(error))) // for debug
      return Promise.reject(error)
    }
  )
}
